<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
    public function index(){
    	if($_COOKIE["manager_id"] == null){
    	
    		$this->login();exit;
    	}else{
    		$_SESSION['manager']['id']=$_COOKIE["manager_id"];
    	}
    	//1.显示总用户数
    	$user = M("User")->count();
    	//2.显示安装工数
    	$comment = M("Comment")->count();
    	//3.本月新增的用户
    	$year = date("Y");
		$month = date("m");
		$allday = date("t");
		$strat_time = strtotime($year."-".$month."-1");
		$end_time = strtotime($year."-".$month."-".$allday);
    	
    	$newuser = M("User")->where("create_time > $strat_time and create_time <$end_time")->count();
    	//获取每个月的订单量
    	
    	setcookie("jan",(int)$jan=$this->getEachOrder($year,1));
    	setcookie("feb",(int)$feb=$this->getEachOrder($year,2));
    	setcookie("mar",(int)$mar=$this->getEachOrder($year,3));
    	setcookie("api",(int)$api=$this->getEachOrder($year,4));
    	setcookie("may",(int)$may=$this->getEachOrder($year,5));
    	setcookie("jun",(int)$jun=$this->getEachOrder($year,6));
    	setcookie("jul",(int)$jul=$this->getEachOrder($year,7));
    	setcookie("agu",(int)$agu=$this->getEachOrder($year,8));
    	setcookie("sep",(int)$sep=$this->getEachOrder($year,9));
    	setcookie("oct",(int)$oct=$this->getEachOrder($year,10));
    	setcookie("nov",(int)$nov=$this->getEachOrder($year,11));
    	setcookie("dec",(int)$dec=$this->getEachOrder($year,12));
    	
    	//4.订单数目
    	$order = M("Order")->count();
    	$this->assign("user",$user);
    	$this->assign("comment",$comment);
    	$this->assign("newuser",$newuser);
    	$this->assign("order",$order);
    	$this->assign("a","500");
        $this->display("index");
    }
   public function getEachOrder($y,$m){
    	$start = strtotime("$y-$m-1");
    	$n = $m+1;
    	$end = strtotime("$y-$n-1")-1;
    	$count = M("Task")->where("pay_status ='pay' and create_time>$start and create_time < $end")->count();
    	return  $count;
    }
    public function login(){
    	$this->display("Index:login");
    }
    public function checkLogin(){
    	$name=$_REQUEST["name"];
    	$p=$_REQUEST["password"];
    	$password = md5("$p");
    	$mana = M("Manage")->where("name = '$name' and password = '$password'")->find();
    	if($mana){
    		$_SESSION['manager']['name']=$mana["managename"];
    		$_SESSION['manager']['id']=$mana["id"];
    		cookie("manager_id",$mana["id"],3600);
    		
    		$this->index();
    	}else{
    		$this->redirect("/Home/index/login_wrong");
    	}
    }
    
    public function login_wrong(){
    	$this->display("login_wrong");
    }
    
    public function logout(){
    	cookie('manager_id',null);
    	$_SESSION["mananger"] = null;
    	
    	$this->display("login");
    }
    
  
  
}